﻿using System.Diagnostics;
using FlyingHat.Core.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace FlyingHat.Extensions.ServiceExtensions
{
    public static class DataBaseService
    {
        public static void AddBaseContext(this IServiceCollection services, string connectionString)
        {
            services.AddDbContextPool<BaseContext>(optionsBuilder =>
            {
                // 设置mysql 连接，设置查询不追踪状态。PS:取消全局状态追踪，增加执行速度
                optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking).UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), i =>
                {
                    // 设置SQL执行超时时间
                    i.CommandTimeout(60);
                });
                // 打印SQL到控制台，这个主要是查看ORM生成的SQL
                optionsBuilder.LogTo(message => Debug.WriteLine(message));
            });
        }
    }
}
